package com.billing.utils;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

	/**
	 * 获取0点
	 * 
	 * @return
	 */
	public static Date getStartTime(int day) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, day);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 根据时间戳获取0点
	 * 
	 * @return
	 */
	public static Date getStartTime(int day, long time) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTimeInMillis(time);
		todayStart.set(Calendar.HOUR_OF_DAY, day);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 获取23点
	 * 
	 * @return
	 */
	public static Date getnowEndTime(int day) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, day);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 根据时间戳获取23点
	 * 
	 * @return
	 */
	public static Date getnowEndTime(int day, long time) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTimeInMillis(time);
		todayEnd.set(Calendar.HOUR_OF_DAY, day);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 将String时间转换为时间戳
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long getStringTime(String time, String format)
			throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = simpleDateFormat.parse(time);
		return date.getTime();
	}

	/**
	 * 根据时间戳获取当前年月日
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringFormat(long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}
	
	/**
	 * 根据时间戳获取当前年月日
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringFormat(long time,String format) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(format);
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}

	/**
	 * 根据时间戳获取当前年月日
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringFormatVehicle(long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}

	/**
	 * 根据时间戳获取当前时分
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringDate(long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}

	/**
	 * 时间累计增长
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static String getGrowth(Date date, int minute) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minute);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(now.getTime());
	}

	/**
	 * 时间累计增长
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static String getReduce(Date date, int minute) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) - minute);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(now.getTime());
	}

	/**
	 * 获取当前年份
	 * 
	 * @return
	 */
	public static int getYear() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 获取当前月份
	 * 
	 * @return
	 */
	public static int getMonth() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH) + 1;
		return month;
	}

	/**
	 * 获取当前日期
	 * 
	 * @return
	 */
	public static int getDate() {
		Calendar cal = Calendar.getInstance();
		int date = cal.get(Calendar.DATE);
		return date;
	}

	/**
	 * 根据时间戳返回日期时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getDay(Long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd");
		Date date = new Date(time);
		String day = simpleDateFormat1.format(date);
		return Integer.valueOf(day);
	}
	/**
	 * 根据时间戳返回日期时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getMonth(Long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MM");
		Date date = new Date(time);
		String day = simpleDateFormat1.format(date);
		return Integer.valueOf(day);
	}
	/**
	 * 根据时间戳返回日期时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getYear(Long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy");
		Date date = new Date(time);
		String day = simpleDateFormat1.format(date);
		return Integer.valueOf(day);
	}

	/**
	 * 获取前一个月最后一天
	 * 
	 * @return
	 */
	public static int getLastDay() {
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		String lastDay = sdf.format(calendar2.getTime());
		return Integer.valueOf(lastDay);
	}

	/**
	 * 获取前一个月月份
	 * 
	 * @return
	 */
	public static int getLastMonth() {
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		String lastDay = sdf.format(calendar2.getTime());
		return Integer.valueOf(lastDay);
	}

	/**
	 * 获取前一个月年份
	 * 
	 * @return
	 */
	public static int getLastYear() {
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String lastDay = sdf.format(calendar2.getTime());
		return Integer.valueOf(lastDay);
	}

	/**
	 * 将字符串转化为日期
	 * 
	 * @param timestr
	 * @return
	 */
	public static Date paraseStringToDate(String timestr) {
		Date date = null;
		Format f = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = (Date) f.parseObject(timestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 根据传入的日期获取所在月份所有日期
	 * 
	 * @param d
	 * @return
	 */
	public static List<String> getAllDaysMonthByDate(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<String> lst = new ArrayList<String>();
		Date date = getMonthStart(d);
		Date monthEnd = getMonthEnd(d);
		while (!date.after(monthEnd)) {
			lst.add(sdf.format(date));
			date = getNext(date);
		}
		return lst;
	}

	private static Date getNext(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		return calendar.getTime();
	}

	private static Date getMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		int index = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DATE, (-index));
		return calendar.getTime();
	}

	private static Date getMonthStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int index = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DATE, (1 - index));
		return calendar.getTime();
	}

	/**
	 * 根据年月日获取当月第一天日期时间戳
	 * 
	 * @param time
	 * @return
	 */
	public static long getFirstDayOfMonth(String time) {
		Date date = paraseStringToDate(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime().getTime();
	}

	/**
	 * 根据年月日获取当月最后一天日期时间戳
	 * 
	 * @param time
	 * @return
	 */
	public static long getLastDayOfMonth(String time) {
		Date date = paraseStringToDate(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime().getTime();
	}

	public static void main(String[] args) throws ParseException {
		// System.out.println(getStartTime(0).getTime());
		// System.out.println(getnowEndTime(23).getTime());
		// long ts=getStartTime(0).getTime();
		// System.out.println(getnowEndTime(23).getTime());
		// System.out.println(getStringFormat(ts));
		// String time=getStringFormat(ts);
		// time +="9:00";
		// System.out.println(getStringTime(time,"yyyy-MM-ddHH:mm"));
		// System.out.println(System.currentTimeMillis());
		// System.out.println(getStringTime("23:00","HH:mm"));
		// System.out.println(getStringTime("00:00","HH:mm"));
		// System.out.println(getStringDate(System.currentTimeMillis()));
		//
		//
		// SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd");
		// Date date = new Date(1516291200000L);
		// System.out.println(simpleDateFormat1.format(date));

		// System.out.println("--------------根据特定的日期‘2014-8-1’获取特定日期所在一个月的每一天--------------");
		// Date d2=paraseStringToDate("2018-2-3");
		// List<String> lst4=getAllDaysMonthByDate(d2);
		// for(String s3:lst4){
		// System.out.println(s3);
		// }
		// System.out.println(Math.abs((1514854800000L-1514865600000L)/60000));
		// System.out.println(getFirstDayOfMonth("2018-1-1"));
		// System.out.println(getLastDayOfMonth("2018-1-1"));
		// System.out.println(getStringFormat(1520179200000L));
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTimeInMillis(1515164400000L);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		System.out.println(todayStart.getTime().getTime());
	}
}
